package com.top.web.hooker.common;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.util.Map;
import java.util.WeakHashMap;

public class ViewManager
{
    interface IFindViewByID
    {
        View findViewById(int id);

        Context getContext();
    }

    public static ViewManager getManager(View root)
    {
        return new ViewManager(root);
    }

    public static ViewManager getManager(Activity root)
    {
        return new ViewManager(root);
    }

    public static ViewManager getManager(Dialog root)
    {
        return new ViewManager(root);
    }

    @SuppressWarnings("unchecked")
    public static <TView extends View> TView find(View v, int id)
    {
        return (TView) v.findViewById(id);
    }

    @SuppressWarnings("unchecked")
    public static <TView extends View> TView find(Activity activity, int id)
    {
        return (TView) activity.findViewById(id);
    }

    private IFindViewByID _root;
    private Map<Integer, View> _viewMap;

    @SuppressLint("UseSparseArrays")
    public ViewManager()
    {
        _viewMap = new WeakHashMap<Integer, View>();
    }

    ViewManager(final View root)
    {
        this();
        _root = new IFindViewByID()
        {
            @Override
            public View findViewById(int id)
            {
                return root.findViewById(id);
            }

            @Override
            public Context getContext()
            {
                return root.getContext();
            }
        };
    }

    ViewManager(final Activity root)
    {
        this();
        _root = new IFindViewByID()
        {
            @Override
            public View findViewById(int id)
            {
                return root.findViewById(id);
            }

            @Override
            public Context getContext()
            {
                return root;
            }
        };
    }

    ViewManager(final Dialog root)
    {
        this();
        _root = new IFindViewByID()
        {
            @Override
            public View findViewById(int id)
            {
                return root.findViewById(id);
            }

            @Override
            public Context getContext()
            {
                return root.getContext();
            }
        };
    }

    View findViewById(View view, int id)
    {
        if (_viewMap.containsKey(id))
            return _viewMap.get(id);

        View child = view.findViewById(id);
        _viewMap.put(id, child);
        return child;
    }

    View findViewById(IFindViewByID view, int id)
    {
        if (_viewMap.containsKey(id))
            return _viewMap.get(id);

        View child = view.findViewById(id);
        _viewMap.put(id, child);
        return child;
    }

    public void setTextColor(View view, int id, int color)
    {
        ((TextView) findViewById(view, id)).setTextColor(color);
    }

    public void setText(View view, int id, String text)
    {
        ((TextView) findViewById(view, id)).setText(text);
    }

    public void setText(View view, int id, int resid)
    {
        ((TextView) findViewById(view, id)).setText(view.getContext().getResources().getString(resid));
    }

    public void setProgress(View view, int id, boolean isloading)
    {
        ((ProgressBar) findViewById(view, id)).setIndeterminate(isloading);
    }

    public void setProgress(View view, int id, int value)
    {
        ((ProgressBar) findViewById(view, id)).setProgress(value);
    }

    public void setVisibility(View view, int id, int visibility)
    {
        findViewById(view, id).setVisibility(visibility);
    }

    public void setAlpha(View view, int id, float alpha)
    {
        findViewById(view, id).setAlpha(alpha);
    }

    public void setBackgroud(View view, int id, Drawable drawable)
    {
        findViewById(view, id).setBackgroundDrawable(drawable);
    }

    public void setBackgroud(View view, int id, int res)
    {
        findViewById(view, id).setBackgroundResource(res);
    }

    public void setTextColor(IFindViewByID view, int id, int color)
    {
        ((TextView) findViewById(view, id)).setTextColor(color);
    }

    public void setText(IFindViewByID view, int id, CharSequence text)
    {
        ((TextView) findViewById(view, id)).setText(text);
    }

    public void setText(IFindViewByID view, int id, int resid)
    {
        ((TextView) findViewById(view, id)).setText(view.getContext().getResources().getString(resid));
    }

    public void setProgress(IFindViewByID view, int id, boolean isloading)
    {
        ((ProgressBar) findViewById(view, id)).setIndeterminate(isloading);
    }

    public void setProgress(IFindViewByID view, int id, int value)
    {
        ((ProgressBar) findViewById(view, id)).setProgress(value);
    }

    public void setImageSource(IFindViewByID view, int id, int srcID)
    {
        ((ImageView) findViewById(view, id)).setImageResource(srcID);
    }

    public void setAlpha(IFindViewByID view, int id, float alpha)
    {
        findViewById(view, id).setAlpha(alpha);
    }

    public void setImageSource(IFindViewByID view, int id, Drawable drawable)
    {
        ((ImageView) findViewById(view, id)).setImageDrawable(drawable);
    }

    public void setVisibility(IFindViewByID view, int id, int visibility)
    {
        findViewById(view, id).setVisibility(visibility);
    }

    public void setBackgroud(IFindViewByID view, int id, Drawable drawable)
    {
        findViewById(view, id).setBackgroundDrawable(drawable);
    }

    public void setBackgroud(IFindViewByID view, int id, int res)
    {
        findViewById(view, id).setBackgroundResource(res);
    }

    public void setButton(IFindViewByID view, int id, String text, View.OnClickListener listener)
    {
        View button = findViewById(view, id);
        if (button instanceof TextView)
            ((TextView)button).setText(text);
        button.setOnClickListener(listener);
    }

    public void setButton(IFindViewByID view, int id, View.OnClickListener listener)
    {
        TextView button = (TextView) findViewById(view, id);
        button.setOnClickListener(listener);
    }

    // ---------------------------------------->

    @SuppressWarnings("unchecked")
    public <TView extends View> TView find(int id)
    {
        return (TView) findViewById(_root, id);
    }

    public void setText(int id, CharSequence text)
    {
        if (_root == null)
            return;

        setText(_root, id, text);
    }

    public void setAlpha(int id, float alpha)
    {
        if (_root == null)
            return;

        setAlpha(_root, id, alpha);
    }

    public void setText(int id, int resid)
    {
        if (_root == null)
            return;

        setText(_root, id, resid);
    }

    public void setTextColor(int id, int color)
    {
        if (_root == null)
            return;

        setTextColor(_root, id, color);
    }

    public void setProgress(int id, boolean isloading)
    {
        if (_root == null)
            return;
        setProgress(_root, id, isloading);
    }

    public void setProgress(int id, int value)
    {
        if (_root == null)
            return;
        setProgress(_root, id, value);
    }

    public void setImageSource(int id, int srcID)
    {
        if (_root == null)
            return;

        setImageSource(_root, id, srcID);
    }

    public void setButton(int id, String text, View.OnClickListener listener)
    {
        if (_root == null)
            return;

        setButton(_root, id, text, listener);
    }

    public void setButton(int id, View.OnClickListener listener)
    {
        if (_root == null)
            return;

        setButton(_root, id, listener);
    }

    public void setImageSource(int id, Drawable drawable)
    {
        if (_root == null)
            return;

        setImageSource(_root, id, drawable);
    }

    public void setVisibility(int id, int visibility)
    {
        findViewById(_root, id).setVisibility(visibility);
    }

    public void setEnabled(int id, boolean enabled)
    {
        findViewById(_root, id).setEnabled(enabled);
    }

    public void getViewHolder(Object holder)
    {
        if (_root != null)
            new ViewFinder().find(_root, holder);
    }

    public void setBackgroud(int id, Drawable drawable)
    {
        findViewById(_root, id).setBackgroundDrawable(drawable);
    }

    public void setBackgroud(int id, int res)
    {
        findViewById(_root, id).setBackgroundResource(res);
    }

    private static int getID(Resources res, String type, String name, String pg)
    {
        return res.getIdentifier(name, type, pg);
    }

    public static String getString(Context context, String name)
    {
        return context.getResources().getString(getID(context.getResources(), "string", name, context.getPackageName()));
    }

    public static Drawable getDrawable(Context context, String name)
    {
        return context.getResources().getDrawable(getID(context.getResources(), "drawable", name, context.getPackageName()));
    }
}
